package com.cyandpos.pad.activity.attachfunction;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.BaseActivity;
import com.cyandpos.pad.attachselect.presenter.IMemberPresenter;
import com.cyandpos.pad.attachselect.presenter.MemberPresenterImpl;
import com.cyandpos.pad.attachselect.view.IMemberView;
import com.cyandpos.pad.member.QueryMemberCentAdapter;
import com.cyandpos.pad.posdata.GetValueCardResponse;
import com.cyandpos.pad.posdata.GetVipCouponResponse;
import com.cyandpos.pad.posdata.QueryMemberInfoResponse;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.ToastUtils;

import butterknife.BindView;

public class MemberCentActivity extends BaseActivity implements IMemberView, View.OnClickListener {
    public static AlertDialog mDialog;//所有dialog
    private static IMemberPresenter mMemberPresenterImpl;
    private ProgressDialog progressDialog;
    @BindView(R.id.recyclerview_querymembercent)
    RecyclerView mRecyclerview;
    private QueryMemberCentAdapter mAdapter;
    @BindView(R.id.tv_dialog_title_name)
    TextView tvtitle_name;
    @Override
    protected void initData() {
        mMemberPresenterImpl = new MemberPresenterImpl(this);
        progressDialog = new ProgressDialog(MemberCentActivity.this);
        showQueryMemberDialog(MemberCentActivity.this,"会员卡积分");
        LinearLayoutManager llm = new LinearLayoutManager(MemberCentActivity.this, LinearLayoutManager.VERTICAL, false);
        mRecyclerview.setLayoutManager(llm);
        mRecyclerview.setItemAnimator(new DefaultItemAnimator());
        mRecyclerview.addItemDecoration(new SpaceItemDecoration(60));
        tvtitle_name.setOnClickListener(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member_cent;
    }


    public void showQueryMemberDialog(final Activity mContext, final String flag) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_member, null);
        TextView tvName = view.findViewById(R.id.tv_dialog_title_name);
        final SelfNumKeyView sf_containt = view.findViewById(R.id.member_soft_keyboard);

        boolean isNeedInputView = false;//是否需要输入布局

        tvName.setText(flag);
        //关闭按钮
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                finish();

            }
        });

        view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_card).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_idcard).setVisibility(View.GONE);

        if (flag.equals("会员卡积分")){
            view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.VISIBLE);
            view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.VISIBLE);
            view.findViewById(R.id.tv_sale_member_card).setVisibility(View.VISIBLE);
            view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.VISIBLE);
            isNeedInputView = true;

        }

        if(!isNeedInputView)
            sf_containt.setVisibility(View.GONE);//不需要输入布局

        //会员积分  刷卡
        view.findViewById(R.id.tv_sale_member_swipe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();

            }
        });
        //会员积分 扫码
        view.findViewById(R.id.tv_sale_member_scan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();

            }
        });
        //卡号
        final EditText etCardNo = view.findViewById(R.id.et_sale_member_card);
        final TextView tvCardNo = view.findViewById(R.id.tv_sale_member_card);

        //手机号
        final EditText etPhone = view.findViewById(R.id.et_sale_member_phone);
        final TextView tvPhone = view.findViewById(R.id.tv_sale_member_phone);

        tvCardNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tvCardNo.setVisibility(View.GONE);
                etCardNo.setVisibility(View.VISIBLE);

                tvPhone.setVisibility(View.VISIBLE);
                etPhone.setVisibility(View.GONE);

                etPhone.setText("");
                etCardNo.setFocusable(true);
                sf_containt.setEnterReceptor(etCardNo);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();
                        ToastUtils.show("卡号开始查询会员积分");
                        Log.i("TAG----NUM:",num);

                        queryCent(num,2); //储值卡号是0
                    }
                });
            }
        });
        CommonUtils.setNoSoftware(etCardNo, mContext,true);
        CommonUtils.setNoSoftware(etPhone, mContext,true);

        tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvPhone.setVisibility(View.GONE);
                etPhone.setVisibility(View.VISIBLE);

                tvCardNo.setVisibility(View.VISIBLE);
                etCardNo.setVisibility(View.GONE);

                etCardNo.setText("");
                etPhone.setFocusable(true);
                sf_containt.setEnterReceptor(etPhone);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();

                        Log.i("TAG----手机号NUM:",num);

                        ToastUtils.show("手机号开始查询会员积分");
                        Log.i("TAG----手机号NUM:",num);

                        queryCent(num,3);

                    }
                });

            }
        });
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();
    }

    public static void queryCent(String content, int type) {
        QueryMemberRequest requestParam = new QueryMemberRequest();
        requestParam.setCondType(type);
        requestParam.setCondValue(content);
        mMemberPresenterImpl.queryMemberCent(requestParam);
    }

    @Override
    public void showMemberDetail(QueryMemberInfoResponse member) {

    }

    @Override
    public void showCouponDetail(GetVipCouponResponse GetVipCouponResponseData) {

    }

    @Override
    public void showCentDetail(QueryMemberInfoResponse response) {
        mAdapter = new QueryMemberCentAdapter(response);
        mRecyclerview.setAdapter(mAdapter);
    }

    @Override
    public void showValueCard(GetValueCardResponse responseData) {

    }

    @Override
    public void showWaitDialog(String content) {
        if (progressDialog != null) {
            progressDialog.setMessage(content);
            if (!progressDialog.isShowing())
                progressDialog.show();
        }
    }

    @Override
    public void showMessage(String content) {
        ToastUtils.showMsg(content,1);

    }

    @Override
    public void dismissWaitDialog() {
        if (progressDialog != null)
            progressDialog.dismiss();
    }
    @Override
    public void onFail(String content, int handleId) {
        ToastUtils.showMsg(content, 0);
    }

    @Override
    public void onFail(String content) {
        ToastUtils.showMsg(content, 0);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_dialog_title_name:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        tvtitle_name.setText("会员积分信息");
    }
}
